//
//  GameDetailTableViewCell_Correlation.m
//  gamebox_qpp
//
//  Created by 赵隆杰 on 2022/9/3.
//

#import "GameDetailTableViewCell_Correlation.h"
#import "GameDetailCorrelationSubView.h"
#import "GameDetailViewController.h"

#define SubViewWidth (SL_WINDOW_WIDTH*(3.7/5.0))
#define SubViewHeight 100

@interface GameDetailTableViewCell_Correlation ()

@property(nonatomic, strong) UIScrollView *scrollView;

@property (nonatomic, strong) NSArray <GameDetailNewModel*>*correlationGames;

@end

@implementation GameDetailTableViewCell_Correlation

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.scrollView = [[UIScrollView alloc] init];
    [self.contentView addSubview:self.scrollView];
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.contentView);
    }];
}

- (void)initCellData:(GameDetailNewModel *)detailModel
{
    _correlationGames = detailModel.correlationGames;
    
    [self.scrollView removeAllSubviews];
    if (_correlationGames.count == 0) {
        return;
    }
    NSInteger unit = 3;
    if (_correlationGames.count == 1 || _correlationGames.count == 2) {
        unit = 1;
    } else if (_correlationGames.count == 3 || _correlationGames.count == 4) {
        unit = 2;
    }
    for (NSInteger i = 0; i < _correlationGames.count; i++) {
        GameDetailCorrelationSubView *subView = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([GameDetailCorrelationSubView class]) owner:self options:nil].firstObject;
        subView.detailModel = _correlationGames[i];
        [self.scrollView addSubview:subView];
        [subView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(SubViewWidth);
            make.height.mas_equalTo(SubViewHeight);
            make.left.mas_equalTo(14 + (SubViewWidth + 10) * (i / unit));
            make.top.mas_equalTo((SubViewHeight + 10) * (i % unit));
        }];
        VHWeakSelf
        [subView addTapBlock:^(id  _Nonnull obj) {
            VHStrongSelf
            GameDetailViewController *vc = [[GameDetailViewController alloc] init];
            vc.modelId = @"1";
            [[UIWindow currentViewController].navigationController pushViewController:vc animated:YES];
        }];
    }
    [self.scrollView mas_updateConstraints:^(MASConstraintMaker *make) {
        NSInteger heightCount = _correlationGames.count;
        if (heightCount > unit) {
            heightCount = unit;
        }
        make.height.mas_equalTo((SubViewHeight + 10) * ((heightCount - 1) % unit) + SubViewHeight);
    }];
    CGFloat scrollSizeWidth = 14 + (SubViewWidth + 10) * ((_correlationGames.count - 1) / unit) + SubViewWidth + 14;
    if (scrollSizeWidth < SL_WINDOW_WIDTH) {
        scrollSizeWidth = SL_WINDOW_WIDTH;
    }
    [self.scrollView setContentSize:CGSizeMake(scrollSizeWidth, 0)];
    
    
}

+(CGFloat)height:(GameDetailNewModel *)detailModel
{
    NSArray <GameDetailNewModel*>*correlationGames = detailModel.correlationGames;
    NSInteger unit = 3;
    if (correlationGames.count == 1 || correlationGames.count == 2) {
        unit = 1;
    } else if (correlationGames.count == 3 || correlationGames.count == 4) {
        unit = 2;
    }
    NSInteger heightCount = correlationGames.count;
    if (heightCount > unit) {
        heightCount = unit;
    }
    CGFloat height = (SubViewHeight + 10) * ((heightCount - 1) % unit) + SubViewHeight;
    return height;
}


@end
